package com.bridgeintelligent.tag.webserver.system.service;

import com.bridgeintelligent.tag.constants.CurrentPage;
import com.bridgeintelligent.tag.constants.PublicConstant;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import com.bridgeintelligent.tag.webserver.system.exception.SystemErrors;
import com.bridgeintelligent.tag.webserver.system.mapper.NoticeMapper;
import com.bridgeintelligent.tag.webserver.system.model.NoticeModel;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayneleo.quickstart.QuickException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @Author：liwei
 * @Date：2023/11/8 14:19
 * @Desc：公告业务处理类
 */
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService{
    @Autowired
    private NoticeMapper mapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpd(NoticeModel model) {
        if(StringUtils.isNotBlank(model.getId())){
            NoticeModel sourceModel = mapper.findOne(model.getId());
            //已启动或者失效状态，不可以编辑
            if(PublicConstant.ONE.equals(sourceModel.getStatus())||PublicConstant.TWO.equals(sourceModel.getStatus())){
                throw QuickException.normal(SystemErrors.ERROR_701005);
            }
            model.setUptTime(DateHelper.currentDateTime());
            mapper.upd(model);
        }else {
            //表中所有数据状态置为2无效
            mapper.updAllStatus();
            //删除关系表中所有的关系数据
            mapper.delNoticeUserRela();
            //新增一条0未开启状态的公告数据
            model.setCreateTime(DateHelper.currentDateTime());
            model.setUptTime(DateHelper.currentDateTime());
            model.setStatus(PublicConstant.ZERO);
            model.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            mapper.add(model);
        }
    }

    @Override
    public void operate(NoticeModel model) {
        if(StringUtils.isBlank(model.getStatus())){
            mapper.del(model.getId());
            mapper.delNoticeUserRelaById(model.getId());
        }else{
            if(PublicConstant.TWO.equals(mapper.findOne(model.getId()).getStatus())){
                throw QuickException.normal(SystemErrors.ERROR_701005);
            }
            mapper.operate(model.getStatus(),model.getId());
        }

    }

    @Override
    public NoticeModel findOneById(String id) {
        return mapper.findOne(id);
    }

    @Override
    public CurrentPage<NoticeModel> list(NoticeModel model) {
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        List<NoticeModel> list = mapper.list(model.getTitle());
        CurrentPage<NoticeModel> CurrentPage = new CurrentPage<>();
        CurrentPage.setContent(list);
        CurrentPage.setTotalPages(page.getPages());
        CurrentPage.setTotalElements(page.getTotal());
        CurrentPage.setSize(page.getPageSize());
        CurrentPage.setRequestPager(model.getPageNumber());
        return CurrentPage;
    }

    @Override
    public NoticeModel findUserNotice() {
        NoticeModel model = new NoticeModel();
        //获取当前登录人信息
        User user = SecurityHelper.currentUser();
        //获取最新的公告信息ID
        String nid = mapper.findLatestNotice();
        if(StringUtils.isNotBlank(nid)){
            String res = mapper.findExist(user.getUserId(),nid);
            if(StringUtils.isBlank(res)){
                mapper.addUserNotice(user.getUserId(),nid, UUID.randomUUID().toString().replaceAll("-", ""));
                model = mapper.findOne(mapper.findLatestNotice());
            }
        }
       return model;
    }
}
